//
//  BaseViewController.swift
//  Entrance
//
//  Created by Chunhui on 2018/1/26.
//  Copyright © 2018年 chunhuiLai. All rights reserved.
//

import UIKit
import RxSwift

class BaseViewController: UIViewController {
    
//    #if TRACE_RESOURCES
//    private let startResourceCount = RxSwift.Resources.total
//    #endif
//    
    var disposeBag = DisposeBag()
    
    lazy var backItem: UIButton = {
        let button = UIButton(type: .custom)
        button.sy_size = CGSize(width: 44, height: 44)
        button.setTitleColor(UIColor.gray, for: UIControlState.highlighted)
        button.setImage(UIImage(named: "back_arrow"), for: UIControlState.normal)
        button.addTarget(self, action: #selector(BaseViewController.back), for: .touchUpInside)
    
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if let nav = navigationController, nav.viewControllers.count > 1 {
            setupBackItem()
        }
        
        configAppearance()
    }
    
    func configAppearance() {
        view.backgroundColor = UIColor.white
    }
    
    func setupBackItem() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backItem)
    }
    
    @objc func back() {
        navigationController?.popViewController(animated: true)
    }
    
    deinit {
//        #if TRACE_RESOURCES
//            print("View controller disposed with \(Resources.total) resources")
//
//            let when = DispatchTime.now() + DispatchTimeInterval.milliseconds(UIApplication.isInUITest ? 1000 : 100)
//
//            mainQueue.asyncAfter (deadline: when) {
//
//                /*
//                 Some small additional period to clean things up. In case there were async operations fired,
//                 they can't be cleaned up momentarily.
//                 */
//                // If this fails for you while testing, and you've been clicking fast, it's ok, just click slower,
//                // this is a debug build with resource tracing turned on.
//                //
//                // If this crashes when you've been clicking slowly, then it would be interesting to find out why.
//                // ¯\_(ツ)_/¯
//                assert(Resources.total <= numberOfResourcesThatShouldRemain, "Resources weren't cleaned properly, \(Resources.total) remained, \(numberOfResourcesThatShouldRemain) expected")
//
//            }
//        #endif
    }
}
